package zheezes.util;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

public class ExceptReg {
	static class Except {
		private Class clazz;
		private String message;

		Except(Class clazz, String message) {
			this.clazz = clazz;
			this.message = message;
		}
		
	
		@Override
		public int hashCode() {
			return clazz.hashCode() + message.hashCode();
		}
		
		@Override
		public boolean equals(Object other) {
			if(other instanceof Except) {
				Except excp = (Except) other;
				if(clazz.equals(excp.clazz)
						&& message.equals(excp.message)) {
					return true;
				}
			}
			return false;
		}
	}
	
	public static final int SOCKET_CONNECTION_RESET = 1;
	public static final int SOCKET_READ_TIMEOUT = 2;
	public static final int CONNECT_REFUSED = 3;
	public static final int SOCKET_CLOSED = 4;

	public static final Map<Except, Integer> map = new HashMap<Except, Integer>();
	
	static {
		map.put(new Except(SocketException.class, "Connection reset"), SOCKET_CONNECTION_RESET);
		map.put(new Except(SocketException.class, "Connection reset by peer: socket write error"), SOCKET_CONNECTION_RESET);
		map.put(new Except(SocketTimeoutException.class, "Read timed out"), SOCKET_READ_TIMEOUT);
		map.put(new Except(ConnectException.class, "Connection refused: connect"), CONNECT_REFUSED);
		map.put(new Except(SocketException.class, "socket closed"), SOCKET_CLOSED);
		map.put(new Except(SocketException.class, "Socket Closed"), SOCKET_CLOSED);
	}

	public static final int get(Exception e) {
		Integer it = map.get(new Except(e.getClass(), e.getMessage()));
		if(it == null) {
			it = -1;
		}
		return it;
	}
}